package com.lct.eq2.test;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.util.Iterator;
import java.util.List;

public class RegQuery {

  private static final int HKEY_CURRENT_USER = 0x80000001;
  private static final int KEY_QUERY_VALUE = 1;
  private static final int KEY_SET_VALUE = 2;
  private static final int KEY_READ = 0x20019;

  public static void main(String args[]) {
//      final Preferences userRoot = Preferences.userRoot();
//      final Preferences systemRoot = Preferences.systemRoot();
//      final Class clz = userRoot.getClass();
//      try {
//          final Method openKey = clz.getDeclaredMethod("openKey", byte[].class, int.class, int.class);
//          openKey.setAccessible(true);
//
//          final Method closeKey = clz.getDeclaredMethod("closeKey", int.class);
//          closeKey.setAccessible(true);
//
//          final Method winRegQueryValue = clz.getDeclaredMethod("WindowsRegQueryValueEx", int.class, byte[].class);
//          winRegQueryValue.setAccessible(true);
//          final Method winRegEnumValue = clz.getDeclaredMethod("WindowsRegEnumValue1", int.class, int.class, int.class);
//          winRegEnumValue.setAccessible(true);
//          final Method winRegQueryInfo = clz.getDeclaredMethod("WindowsRegQueryInfoKey1", int.class);
//          winRegQueryInfo.setAccessible(true);
//
//
//          byte[] valb = null;
//          String vals = null;
//          String key = null;
//          Integer handle = -1;
//
//          //Query Internet Settings for Proxy
//          key = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
//          handle = (Integer) openKey.invoke(userRoot, toCstr(key), KEY_READ, KEY_READ);
//          valb = (byte[]) winRegQueryValue.invoke(userRoot, handle.intValue(),
//                  toCstr("ProxyServer"));
//          vals = (valb != null ? new String(valb).trim() : null);
//          System.out.println("Proxy Server = " + vals);
//          valb = (byte[]) winRegQueryValue.invoke(userRoot, handle.intValue(),
//                  "ProxyEnable");
//          vals = (valb != null ? new String(valb).trim() : null);
//          System.out.println("Proxy Enabled = " + vals);
//          closeKey.invoke(Preferences.userRoot(), handle);
//
//      } catch (Exception e) {
//          e.printStackTrace();
//      }
      
      try {
            System.setProperty("java.net.useSystemProxies","true");
            List l = ProxySelector.getDefault().select(
                        new URI("http://www.yahoo.com/"));
            
            for (Iterator iter = l.iterator(); iter.hasNext(); ) {
                Proxy proxy = (Proxy) iter.next();
                System.out.println("proxy hostname : " + proxy.type());
                
                InetSocketAddress addr = (InetSocketAddress)
                    proxy.address();
                
                if(addr == null) {
                    System.out.println("No Proxy");
                } else {
                    System.out.println("proxy hostname : " + 
                            addr.getHostName());
                    System.out.println("proxy port : " + 
                            addr.getPort());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
      
  }

   private static byte[] toCstr(String str) {
       byte[] result = new byte[str.length() + 1];
       for (int i = 0; i < str.length(); i++) {
           result[i] = (byte) str.charAt(i);
       }
       result[str.length()] = 0;
       return result;
   }
}